function play() {
    //默认事件的绑定与取消
    defaultEvent('add');

    function defaultEvent(type) {
        var event = ['touchstart', 'touchmove', 'touchend'];
        if (type == 'add') {
            for (var i in event) {
                $(document).on(event[i], function (e) {
                    events(e)
                }, false);
            }
        } else if (type == "remove") {
            for (var i in event) {
                $(document).unbind(event[i]);
                console.log(2);
            }
        }

        function events(e) {
            e.preventDefault();
            return false;
        }
    }
    var long = 0,
        lang = 0;
    //拖拽实现
    $('.moveBox .mover').drag({
        process: function (e) {
            long = $('.moveBox .mover').offset().left + $('.moveBox .mover').width() / 2; //拖拽物品的中心线
            lang = $('.moveBox .mover').offset().top + $('.moveBox .mover').height(); //拖拽物品的高度
        }
    });
    var arr = ['http://cdn.yhq.lenovo.com.cn/school/img/computer1.png', 'http://cdn.yhq.lenovo.com.cn/school/img/computer2.png', 'http://cdn.yhq.lenovo.com.cn/school/img/giftBag.png'],
        score = [0, 0, ],
        listKey = ['联想电脑', '福袋'];
    //生成东西
    function gift(top, left, index) {
        //第一步创建对象
        this.top = top;
        this.left = left;
        this.index = index;
        this.timer = null;
        this.rate=1;
        //第二步创建节点
        this.Create = function () {
            //生成节点
            this.dom = document.createElement('img');
            //设置属性
            this.dom.setAttribute("index", this.index);
            this.dom.setAttribute("class", "goods");
            if (this.index == 1) {
                this.dom.setAttribute("class", "goods1");
            } else if (this.index == 0) {
                this.dom.setAttribute("class", "goods0");
            }
            this.dom.setAttribute("src", arr[this.index]);
            //获取父节点
            var parent = document.querySelector(".game");
            parent.appendChild(this.dom);
            this.dom.style.left = this.left + '%';
            this.dom.style.top = this.top + '%';
        }
        //第三步动画
        this.move = function () {
                var self = this; //备份this
                this.timer = setInterval(function () {
                    //self.rate+=0.09;
                    self.top+=self.rate;
                    if (self.top > 100) {
                        self.die(); //消除多余节点
                    }
                    //判断高度符合条件后再去判断距左距离
                    var nowtop = self.dom.offsetTop + self.dom.height,
                        short = lang - nowtop;

                    if (short >= 0 && short <= 60 && nowtop < lang) { //高度判断条件
                        // console.log(lang+'&'+nowtop);
                        self.statistics();
                    }

                    self.dom.style.top = self.top + '%';
                }, 30);
            },
            this.die = function () {
                clearInterval(this.timer);
                var parent = document.querySelector(".game");
                parent.removeChild(this.dom);
            },
            this.statistics = function () {
                var self = this, //备份this
                    nowleft = self.dom.offsetLeft + self.dom.width / 2; //下落物品中线

                var lshart = long - nowleft; //物品距左差值
                var nowtop = self.dom.offsetTop + self.dom.height;
                if (lshart >= -15 && lshart <= 15) {
                    this.BGrander();
                    self.die();
                }

            },
            this.BGrander = function () {
                var self = this;
                var ide = parseInt(self.dom.getAttribute("index"));
                compute(ide);

                function compute(order) {
                    if (order == 0 || order == 1) {
                        ind = 0;
                    } else {
                        ind = 1;
                    }
                    score[ind] += 1;
                    $("body .add").show();
                    setTimeout(function () {
                        $("body .add").hide(500);
                    }, 1000);
                    var list = 'body .list' + ind;
                    $(list).html(score[ind]);
                    var max = Math.max.apply(null, score);
                    for (var i in score) {
                        if (score[i] == max) {
                            $('.result .maxCount').html(listKey[i]);
                            break;
                        }
                    }
                    var sum = score.reduce(function (prev, cur, index, array) {
                        return prev + cur;
                    });
                    $("body .giftCount").html(sum);
                    $(".resultInfo .sum").html(sum);
                }
            }
        this.Create();
        this.move();
    }

    var randerTimer = null,
        randerTimer1 = null,
        randerTimer2 = null,
        randerTimer3 = null;
    var newtimer = setInterval(function () {
        //max===75 min=3 *73+3
        var ha = new gift(0, (Math.random() * 73 + 3), parseInt(Math.random() * 3, 10));
        randerTimer = setTimeout(function () {
            var ha = new gift(0, (Math.random() * 73 + 3), parseInt(Math.random() * 3, 10));
        }, 400)

        randerTimer1 = setTimeout(function () {
            var ha = new gift(0, (Math.random() * 73 + 3), parseInt(Math.random() * 3, 10));
        }, 800)

        randerTimer2 = setTimeout(function () {
            var ha = new gift(0, (Math.random() * 73 + 3), parseInt(Math.random() * 3, 10));
        }, 1200)
        randerTimer3 = setTimeout(function () {
            var ha = new gift(0, (Math.random() * 73 + 3), parseInt(Math.random() * 3, 10));
        }, 1600)

    }, 100000 / 50)

    /*时间信息*/
    var total = 60;
    var xx = setInterval(function () {
        total--;
        console.log(total);
        if (total>=1 && total<= 5) {
            $(".game .daojishi").show();
            var str = 'http://cdn.yhq.lenovo.com.cn/school/img/daojishi' + total + '.png';
            $(".game .daojishi").attr("src", str);
            if(total==1){
                setTimeout(function(){
                $(".game .daojishi").hide();
                },1000)
                $("body .musicBox").hide();
                var audio = document.getElementById('music');
                if (audio.played) {
                    audio.pause(); // 停止
                }
            }
        }
        $(".game .timeOut").html(total + 's');
        if (total < 0) {
            clearInterval(randerTimer);
            clearInterval(randerTimer1);
            clearInterval(randerTimer2);
            clearInterval(randerTimer3);
            clearInterval(newtimer);
            clearInterval(xx);

            $(".game").hide();
            //获取排行榜
            getRank();
            $(".result").show();
            
            var scoreLast = score;
            var temp = '';
            for (var i in scoreLast) {
                if (i == scoreLast.length - 1) {
                    temp += (parseInt(i) + 3) + '_' + scoreLast[i];
                } else {
                    temp += (parseInt(i) + 3) + '_' + scoreLast[i] + ',';
                }

            }
            localStorage.setItem('score', temp);
            //打开默认事件
            defaultEvent('remove');
            /**向后台传递数据 */
            $.ajax({
                url: globalVar.path + "/v1/lenovo/lucky2018/saveScoreRecord.do",
                dataType: "jsonp",
                type: "post",
                data: {
                    'inputScore': localStorage.getItem('score')
                },
                success: function (data) {

                    if (data.result == "ok") {
                        globalVar.remainPlayNumber = data.values;
                        localStorage.setItem('user', JSON.stringify(globalVar));

                    } else {}
                },
                error: function (data) {}
            });

        }
    }, 1000)
}

/**获取排行榜数据 */
function getRank() {

    $.ajax({
        url: globalVar.path + "/v1/lenovo/lucky2018/getScoreSort10.do",
        dataType: "jsonp",
        type: "post",
        success: function (data) {
            if (data.result == "ok") {
                var str = "";
                for (var i in data.values) {
                    var name=data.values[i].nickname;
                    if(name.length>8){
                        name=name.substr(0,8)+"...";
                    }
                    str += "<li><span>" + name + "</span><span>接货总数：<sapn class='total'>" + data.values[i].score + "</sapn></span></li>";
                }
                $("body .rankTotal").html(str);
            } else {}
        },
        error: function (data) {}
    });
}